// pages/login/login.js
import {
  getHome
} from '../../apis/api'
import WxValidate from '../../utils/WxValidate'
import crypt from '../../utils/crypt'
var guiCode = require('../../utils/guiCode.js');
var md5 = require('md5')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginForm: {
      telephone: '',
      password: ''
    },
    guiCode: '',
    checked: false,
    disabled:true,
  },
  loginFormPhone: function (e) {
    this.setData({
      ['loginForm.telephone']: e.detail.value
    })
  },

  loginFormPassword: function (e) {
    this.setData({
      ['loginForm.password']: e.detail.value
    })
  },
  // login:async function() {
  //   const res = await getHome(this.data.loginForm)
  //   if(res.code!==200) {
  //     // return wx.showToast({
  //     //   title: '登录失败',
  //     //   icon: 'error',
  //     //   duration: 2000
  //     // })
  //     return false
  //   }
  //   wx.setStorageSync('token', res.data.tokenValue)
  //   wx.setStorageSync('userId', res.data.loginId)
  //   wx.switchTab({
  //     url: '/pages/myvip/myvip',
  //   })

  // },
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },
  checkboxChange:function(e){
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    let val = e.detail.value
    if(val.length>0){
      this.setData({
        checked:true,
        disabled:false
      })
    }else{
      this.setData({
        checked:false,
        disabled:true
      })
    }
  },
    // 跳转页面 隐私协议
    agreement:function(){
      wx.navigateTo({
        url: '/pages/Agreement/Agreement'
      })
    },
  initValidate() {
    const rules = {
      telephone: {
        required: true,
        tel: true
      },
      password: {
        required: true
      }
    }
    const messages = {
      telephone: {
        required: '请输入手机号',
        tel: '请输入正确的手机号'
      },
      password: {
        required: '请输入密码'
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },
  formSubmit: async function (e) {
    const params = e.detail.value
    //校验表单
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      this.showModal(error)
      return false
    }
    this.validateGuiCode()
    if(!this.validateGuiCode()) {
      return false
    }
    
    const res = await getHome({
      telephone: this.data.loginForm.telephone,
      password: crypt.encrypt(md5(this.data.loginForm.password))
      // password: this.data.loginForm.password
    })
    console.log(res);
    if (res.code !== 200) {
      this.guiCodeTap()
      return wx.showToast({
        title: `密码错误`,
        icon: 'error',
        duration: 2000
      })
    }
    wx.setStorageSync('token', res.data.tokenValue)
    wx.setStorageSync('userId', res.data.loginId)
    wx.showToast({
      title: '登录成功',
      icon: 'success',
      duration: 1000
    })
    setTimeout(() => {
      wx.switchTab({
        url: '/pages/myvip/myvip',
      })
    }, 1000)
  },
  bindinputCode(e) {
    this.setData({
      guiCode: e.detail.value
    })
  },

  /**
   * 刷新图形验证码
   */
  guiCodeTap() {
    this.guiCode.refresh()
  },

  /**
   * 验证图形验证码
   */
  validateGuiCode() {
    let res = this.guiCode.validate(this.data.guiCode)
    if (!this.data.guiCode) {
      wx.showToast({
        title: '请输入图形验证码',
        icon: 'none'
      })
      this.guiCodeTap()
      return false
    } else if (!res) {
      wx.showToast({
        title: '图形验证码错误',
        icon: 'none'
      })
      this.guiCodeTap()
      return false
    } else return true
  },

  /**
   * 立即验证按钮监听
   */
  toValidate() {
    //验证图形验证码
    this.validateGuiCode()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
    this.guiCode = new guiCode({
      el: '#guiCodeCanvas',
      width: 70,
      height: 50,
      createCodeImg: ""
    })
  }
})